package com.liu.mysqldemo.controller;

import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.liu.mysqldemo.entity.Teacher;
import com.liu.mysqldemo.repository.TeacherRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class TeacherController {

    @Resource
    TeacherRepository teacherRepository;

    @GetMapping(value = "/getTeachers",produces = {"application/json;charset=utf-8"})
    public String getTeachers(Integer id,Integer pageIndex,Integer pageSize){
        String result = "";
        Sort sort = Sort.by(Sort.Direction.DESC,"age");
        PageRequest pageRequest = PageRequest.of(pageIndex,pageSize,sort);
        Page<Map<String,Object>> page = teacherRepository.findById(id,pageRequest);
        result = new Gson().toJson(page);
//        Map<String,Object> map = new HashMap<>();
//        map.put("totalPages",page.getTotalPages());//总页数
//        map.put("total",page.getTotalElements());//总数
//        map.put("items",page.getContent());//数据内容
//        JSON.toJSONString(map);

        return result;
    }
}
